<template>
  <div>
    <el-dialog
      title="忘记密码"
      :visible.sync="dialogVisible"
      width="35%"
      :modal="false"
      :top="'15%'"
      @close="handleDialogClose"
    >
      <el-form
        ref="forgetPasswordForm"
        :model="forgetPasswordForm"
        label-width="80px"
      >
        <el-form-item label="手机号" prop="phonenumber">
          <el-input v-model="forgetPasswordForm.phonenumber"></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="verificationCode">
          <el-input v-model="forgetPasswordForm.verificationCode"></el-input>
          <el-button
            :disabled="countdown > 0"
            @click="sendVerificationCode"
            style="margin-top: 10px"
          >
            {{ countdown > 0 ? `${countdown}秒后重新发送` : "发送验证码" }}
          </el-button>
        </el-form-item>
        <!-- 其他相关字段和操作 -->
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="handleDialogClose">取消</el-button>
        <el-button type="primary" @click="submitForgetPasswordForm"
          >确定</el-button
        >
      </span>
    </el-dialog>
    <UpdatePassWordDialog ref="UpdatePassWordDialog"></UpdatePassWordDialog>
  </div>
</template>

<script>
import UpdatePassWordDialog from "./UpdatePassWordDialog.vue";

export default {
  components: { UpdatePassWordDialog },
  data() {
    return {
      dialogVisible: false,
      forgetPasswordForm: {
        phonenumber: "",
        verificationCode: "",
        // 其他相关字段
      },
      countdown: 0,
    };
  },

  methods: {
    showForgetPasswordDialog() {
      this.dialogVisible = true;
    },
    handleDialogClose() {
      this.dialogVisible = false;
      // 清空表单数据
      this.$refs.forgetPasswordForm.resetFields();
      // 重置倒计时
      this.countdown = 0;
    },
    sendVerificationCode() {
      // 模拟发送验证码的后台逻辑
      console.log("发送验证码");

      const verifyPhoneNumber = /^1[3456789]\d{9}$/;
      //验证手机号合法性
      if (!verifyPhoneNumber.test(this.forgetPasswordForm.phonenumber)) {
        this.$message({
          message: "手机号不合法，请输入11位的数字的手机号",
          type: "error",
        });
        return false;
      }

      //发送验证码请求
      this.$request.getHttp({
        url: "/sms/sendMessage",
        params: {
          number: this.forgetPasswordForm.phonenumber,
          prefix: "forgot",
        },
        success: (data) => {
          console.log(data);
        },
        error: (error) => {},
      });

      // 开始倒计时
      this.countdown = 60;
      const timer = setInterval(() => {
        if (this.countdown > 0) {
          this.countdown--;
        } else {
          clearInterval(timer);
        }
      }, 1000);
    },
    //提交验证
    submitForgetPasswordForm() {
      //校验格式
      if (!this.verifyFormat()) {
        return;
      }

      //验证验证码是否正确
      this.$request.postHttp({
        url: "/verificationcode/checkCode",
        data: {
          phonenumber: this.forgetPasswordForm.phonenumber,
          verificationCode: this.forgetPasswordForm.verificationCode,
        },
        success: (data) => {
          if (data.code == 200) {
            if (!data.pass) {
              return;
            }
            localStorage.setItem(
              "phonenumber",
              this.forgetPasswordForm.phonenumber
            );
            this.handleDialogClose();
            this.$refs.UpdatePassWordDialog.showChangePasswordDialog();
          } else if (data.code == 500) {
            this.$message({
              message: data.msg,
              type: "error",
            });
          }
        },
        error: (error) => {},
      });
    },
    //验证格式
    verifyFormat() {
      const verifyPhoneNumber = /^1[3456789]\d{9}$/;
      const verifyCode = /^\d{6}$/;

      //验证手机号合法性
      if (!verifyPhoneNumber.test(this.forgetPasswordForm.phonenumber)) {
        this.$message({
          message: "手机号不合法，请输入11位的数字的手机号",
          type: "error",
        });
        return false;
      }
      //验证验证码合法性
      if (!verifyCode.test(this.forgetPasswordForm.verificationCode)) {
        this.$message({
          message: "请输入6位数字的验证码",
          type: "error",
        });
        return false;
      }
      return true;
    },
  },
};
</script>

<style scoped>
</style>
